package com.uchedao.vehicledetection.view.accident;

import android.content.ContentValues;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import com.nostra13.universalimageloader.utils.L;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.adapter.DetectionAdapter;
import com.uchedao.vehicledetection.adapter.DetectionAdapter.DetectionSingleAdapterListener;
import com.uchedao.vehicledetection.db.DbHelper;
import com.uchedao.vehicledetection.global.GlobalInfo;
import com.uchedao.vehicledetection.loadstate.UCheDaoRelativeLayout;
import com.uchedao.vehicledetection.model.Accident;
import com.uchedao.vehicledetection.model.Detection;
import com.uchedao.vehicledetection.model.Draft;
import com.uchedao.vehicledetection.model.ResultInt;
import com.uchedao.vehicledetection.utils.Util;
import com.uchedao.vehicledetection.view.widget.stickylistheaders.StickyListHeadersListView;

import java.util.ArrayList;

/**
 * Description:加强件view
 * <p/>
 * Date: 2015-09-16  10:07
 * User: wushan
 */
public class AccidentReinforcementView extends UCheDaoRelativeLayout {


    private Accident accident;

    public AccidentReinforcementView(Context context) {
        super(context);
    }

    public AccidentReinforcementView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AccidentReinforcementView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean save() {

        ContentValues values = new ContentValues();
        values.put(DbHelper.KEY_DRAFT_ACCIDENT, toJson(accident));
        accident.reinforcementInt = 1;

        int status = GlobalInfo.draft.status[Draft.TYPE_ACCIDENT];

//        System.out.println("ws----accident.reinforcementInt="+accident.reinforcementInt);
//        System.out.println("ws----accident.coverInt="+accident.coverInt);
//        System.out.println("ws----accident.skeletonsInt="+accident.skeletonsInt);

        if( accident.coverInt +accident.reinforcementInt+accident.skeletonsInt==3){
            values.put(DbHelper.KEY_DRAFT_ACCIDENT_STATUS, Draft.STATUE_FINISH);
            status = Draft.STATUE_FINISH;
        }

        // 若原先状态为未完成，且数据已变动，则状态改变为已完成
//        if (GlobalInfo.draft.status[Draft.TYPE_ACCIDENT] != Draft.STATUE_FINISH) {
//            values.put(DbHelper.KEY_DRAFT_ACCIDENT_STATUS, Draft.STATUE_FINISH);
//            status = Draft.STATUE_FINISH;
//        }

        // 保存成功
        if (GlobalInfo.dbHelper.updateDraf(GlobalInfo.draft.car_plate, values)) {
            GlobalInfo.draft.accident = accident.clone();
            GlobalInfo.draft.status[Draft.TYPE_ACCIDENT] = status;
            return true;
        }

        return false;
    }

    @Override
    public boolean dataChanged() {
        return !toJson(accident).equals(toJson(GlobalInfo.draft.accident));
//        return false;
    }

    @Override
    public void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.view_detection, this);
        StickyListHeadersListView stickListHeadersListView = (StickyListHeadersListView) findViewById(R.id.detection_listView);
        DetectionAdapter coverAdapter = new DetectionAdapter(mContext, getReinforcementData(), new DetectionSingleAdapterListener() {
            @Override
            public void onSingleResult(int position, int value) {
                accident.reinforcement[position].result = value;
                L.i("accident:" + accident.reinforcement.toString());
            }
        });
        coverAdapter.setResultTvWidth(Util.sp2px(mContext, 18) * 7);
        stickListHeadersListView.setAdapter(coverAdapter);
    }

    @Override
    public void initDataAfterView() {

    }

    @Override
    public void initListener() {

    }


    /**
     * 得到加强件数据
     *
     * @return
     */
    private ArrayList<Detection> getReinforcementData() {
        ArrayList<Detection> array = new ArrayList<Detection>();

        Detection detection = null;
        String header = mContext.getString(R.string.check_part);
        String[] texts = GlobalInfo.resources.getStringArray(R.array.reinforcement_values);

        accident = GlobalInfo.draft.accident.clone();

        for (int i = 0; i < texts.length; i++) {
            if (accident.reinforcement[i] == null) {
                accident.reinforcement[i] = new ResultInt();
                accident.reinforcement[i].name = texts[i];
            }
            detection = new Detection(Detection.TYPE_THREE_SELECT);
            detection.setHeader(header);
            detection.setText(texts[i]);
            detection.setRequestFocus(true);
            detection.setResId(R.array.reinforcement_select_entries);
            detection.setValue(accident.reinforcement[i].result);
            array.add(detection);
        }
        return array;
    }

}
